package com.yc.home.cloudother.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yc.entity.Doquestion;
import com.yc.home.cloudother.mapper.DoquestionMapper;
import com.yc.home.cloudother.service.IDoquestionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.sql.Timestamp;
import java.time.LocalDateTime;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author lzk
 * @since 2024-09-23
 */
@Service
public class DoquestionServiceImpl extends ServiceImpl<DoquestionMapper, Doquestion> implements IDoquestionService {

    @Autowired
    DoquestionMapper doquestionMapper;

    //新增刷题表
    @Override
    public void add(String uid, String qid, String tid, String answer) {
        //判断有没有，如果有则将status减去1
        LambdaQueryWrapper<Doquestion> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Doquestion::getUid, uid);
        lqw.eq(Doquestion::getQid, qid);
        Doquestion doquestion = doquestionMapper.selectOne(lqw);
        if(doquestion != null){
            //有数据
            if(doquestion.getStatus() > 0){
                doquestion.setStatus(doquestion.getStatus() - 1);
                doquestion.setAnswer(answer);
                doquestion.setTf(1);
                doquestion.setCreatetime(Timestamp.valueOf(LocalDateTime.now()));
                doquestion.setSum(doquestion.getSum() + 1);
                doquestionMapper.updateById(doquestion);
            }else{
                doquestion.setAnswer(answer);
                doquestion.setTf(1);
                doquestion.setCreatetime(Timestamp.valueOf(LocalDateTime.now()));
                doquestion.setSum(doquestion.getSum() + 1);
                doquestionMapper.updateById(doquestion);
            }
        }else{
            //插入数据
            Doquestion doquestion1 = new Doquestion();
            doquestion1.setUid(Integer.valueOf(uid));
            doquestion1.setQid(Integer.valueOf(qid));
            doquestion1.setTid(Integer.valueOf(tid));
            doquestion1.setAnswer(answer);
            doquestion1.setStatus(0);
            doquestion1.setTf(1);
            doquestion1.setSum(1);
            doquestionMapper.insert(doquestion1);
        }
    }

    @Transactional
    @Override
    public void addno(String uid, String qid, String tid, String answer) {
        //判断有没有，如果有则将status减去1
        LambdaQueryWrapper<Doquestion> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Doquestion::getUid, uid);
        lqw.eq(Doquestion::getQid, qid);
        Doquestion doquestion = doquestionMapper.selectOne(lqw);
        if(doquestion != null){
            //有数据
            doquestion.setStatus(doquestion.getStatus() + 1);
            doquestion.setAnswer(answer);
            doquestion.setSum(doquestion.getSum() + 1);
            doquestion.setCreatetime(Timestamp.valueOf(LocalDateTime.now()));
            doquestion.setTf(0);
            doquestionMapper.updateById(doquestion);
        }else{
            //插入数据
            Doquestion doquestion1 = new Doquestion();
            doquestion1.setUid(Integer.valueOf(uid));
            doquestion1.setQid(Integer.valueOf(qid));
            doquestion1.setTid(Integer.valueOf(tid));
            doquestion1.setAnswer(answer);
            doquestion1.setSum(1);
            doquestion1.setTf(0);
            doquestion1.setStatus(1);
            doquestionMapper.insert(doquestion1);
        }
    }
}
